package org.example;

import java.util.concurrent.atomic.AtomicInteger;

public class UnsafeCounter {
    private static AtomicInteger count = new AtomicInteger(0);

    public synchronized static void increment() {
        count.incrementAndGet();
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t3 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t4 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t5 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t6 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t7 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t8 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t9 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        Thread t10 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                UnsafeCounter.increment();
            }
        });
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        t7.start();
        t8.start();
        t9.start();
        t10.start();
        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();
            t5.join();
            t6.join();
            t7.join();
            t8.join();
            t9.join();
            t10.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(count);
    }
}

